/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package flightlifter;

import java.util.Vector;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author Matt
 */
public abstract class InputOutputNode extends WiringNode{

    private boolean inputSet = false;

    private float value = 0 ;
    private WiringConsumer consumer = null;

    private int type;
    private int consumerIndex;

    public void setConsumer(WiringConsumer consumer) {
        this.consumer = consumer;
    }

    public void invalidate(){
        finished = false;
        inputSet = false;
    }

    public InputOutputNode(int x, int y, TrueTypeFont font) {
        this.font = font;
        locX = x ;
        locY = y;

    }

    public void evaluate() {
        //don't do anything until our parent node has set our value
        //if no output, then don't wait for this node
        if (consumer == null){
            finished = true;
        }else if (inputSet){
            value = -value;
            consumer.setValue(value);
            finished = true;
        }

    }

    public void lineToConsumer() {

        if (consumer != null) {
            PVector center = consumer.getScreenCenter();

            GL11.glColor3f(1,1,0);

            GL11.glPushMatrix();

            GL11.glMatrixMode(GL11.GL_PROJECTION);                        // Select The Projection Matrix
            GL11.glLoadIdentity();                                   // Reset The Projection Matrix
            GL11.glOrtho(0, 1280, 0, 1024, -1, 1);                          // Set Up An Ortho Screen
            GL11.glMatrixMode(GL11.GL_MODELVIEW);                         // Select The Modelview Matrix
            GL11.glLoadIdentity();                                   // Reset The Modelview Matrix

            GL11.glBegin(GL11.GL_LINES);
            GL11.glVertex3f(locX+cWidth/2, locY,0);
            GL11.glVertex3f(center.x-cWidth/2, center.y,0);

            GL11.glEnd();
            GL11.glPopMatrix();
        }

    }

    public void hookUp(Vector<Node> constraints,
            Vector<WiringNode> wiring) {

        //-1 means this node wasn't hooked up to anything on save
        if (consumerIndex != -1) {
            if (type == 0) {
                setConsumer((WiringConsumer) wiring.elementAt(consumerIndex));
            }
            else {
                setConsumer((WiringConsumer) constraints.elementAt(consumerIndex));
            }
        }
    }
/*
    public InverterNode(String[] line, TrueTypeFont font) {
        this(Integer.parseInt(line[1]),
               Integer.parseInt(line[2]),
                font);

        type = Integer.parseInt(line[3]);
        consumerIndex = Integer.parseInt(line[4]);
    }
    public String pickle(Vector<Node> constraints,
            Vector<WiringNode> wiring) {

        String ret = "I,"
                + x + ","
                + y + ",";

        if (consumer instanceof WiringNode) {
            ret += 0 + "," + wiring.indexOf(consumer);
        }
        else {
            ret += 1 + "," + constraints.indexOf(consumer);
        }
        return ret;
    }
*/
    public void setValue(float value) {
        this.value = value;
        inputSet = true;
    }




}


